package com.appheader.rongmaphone.constants;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * 应用全局变量
 *
 * @author alaowan
 */
public class GlobalVars {
    private static Properties sProps;
    private static Context sContext;
    public static boolean isDebug;
    // 是否使用本地模拟数据
    public static boolean useSimulateData;

    private static float sDensity = -1;
    private static int sScreenWidth = -1;
    private static int sScreenHeight = -1;
    public static final String EXTRA_CURRENT = "current";
    public static final String EXTRA_MAX = "maxLen";

    public static final String ACTION_PLAY_PRG = "com.qf.broadcast.play.prg";
    public static final String ACTION_PLAY_SEEK = "com.qf.broadcast.play.seek";

    private static String BASEURL = "http://www.armyhorse.com";

    public static void init(Context c) {
        sContext = c;
        loadProperties();
        isDebug = Boolean.valueOf(sProps.getProperty("isDebug", "false"));
        useSimulateData = Boolean.valueOf(sProps.getProperty("useSimulateData", "false"));
    }

    public static Context getContext() {
        return sContext;
    }

    public static Properties getAppConfiguration() {
        return sProps;
    }

    public static boolean isDebug() {
        return isDebug;
    }

    /**
     * 获取应用服务器URL
     *
     * @return
     */
    public static String getAppServerUrl() {
        return sProps.getProperty("appserver.url", "");
    }

    /**
     * 所有请求到服务，所需的共同请求字段
     *
     * @return
     */
    //意见反馈
    public static String getSuggestionUrl() {
        return BASEURL + "/rest/fb/feedback.do?method=createFeedBack";
    }
    //登陆
    public static String getAppBaseUrl() {
        return BASEURL + "/rest/auth/login.do";
    }

    //首页
    public static String getAppCloudUrl() {
        return BASEURL + "/rest/busiclient/project/project.do?method=getProjectList";
    }

    //详情页
    public static String getAppContentCloudUrl() {
        return BASEURL + "/rest/busiclient/client/download.do?method=getProjectData";
    }

    //UserList
    public static String getAppCloudUserListUrl(){
        return BASEURL+"/rest/busiclient/user/user.do?method=getUserInfo";
    }
    //每一页的
    public static String getAppContentPageUrl() {
        return BASEURL + "/rest/busiclient/page/page.do?method=getPage";
    }

    //zip包
    public static String getAppZipUrl() {
        return BASEURL + "/rest/busiclient/client/download.do?method=downloadPreviewZip";
    }


    //退出登录
    public static String getAppLogOut() {
        return BASEURL + "/rest/auth/logout.do";
    }

    /**
     * 获取应用私有目录
     *
     * @return
     */
    @SuppressLint("SdCardPath")
    public static String getAppFilesDir() {
        return "/data/data/" + sContext.getPackageName();
    }

    public static int getVersionCode() {
        return getPackageInfo().versionCode;
    }

    public static String getVersionName() {
        return getPackageInfo().versionName;
    }

    /**
     * 获取应用全局设置的preference
     */
    public static SharedPreferences getSettingsPref() {
        return sContext.getSharedPreferences("settings", Context.MODE_PRIVATE);
    }

    public static PackageInfo getPackageInfo() {
        PackageManager pm = sContext.getPackageManager();
        PackageInfo info = null;
        try {
            info = pm.getPackageInfo(sContext.getPackageName(), PackageManager.GET_CONFIGURATIONS);
        } catch (NameNotFoundException e) {
        }
        return info;
    }

    private static Properties loadProperties() {
        sProps = new Properties();
        try {
            int id = sContext.getResources().getIdentifier("config", "raw", sContext.getPackageName());
            sProps.load(sContext.getResources().openRawResource(id));
        } catch (Exception e) {
            Log.e(GlobalVars.class.getName(), "Could not find the properties file.");
        }
        return sProps;
    }

    //获取手机状态栏高度
    public static int getStatusBarHeight(Context context) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

    //真实的屏幕高度
    public int getRealScreenHeight() {
        int heightPixels;
        WindowManager w = ((Activity) getContext()).getWindowManager();
        Display d = w.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        d.getMetrics(metrics);
        // since SDK_INT = 1;
        heightPixels = metrics.heightPixels;
        // includes window decorations (statusbar bar/navigation bar)
        if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17)
            try {
                heightPixels = (Integer) Display.class
                        .getMethod("getRawHeight").invoke(d);
            } catch (Exception ignored) {
            }
            // includes window decorations (statusbar bar/navigation bar)
        else if (Build.VERSION.SDK_INT >= 17)
            try {
                android.graphics.Point realSize = new android.graphics.Point();
                Display.class.getMethod("getRealSize",
                        android.graphics.Point.class).invoke(d, realSize);
                heightPixels = realSize.y;
            } catch (Exception ignored) {
            }
//		LogUtil.error("realHightPixels-heightPixels", heightPixels + "width");
        return heightPixels;
    }
}
